<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/10/12
 * Time: 11:34
 */
namespace app\api\controller;
use language\Languagemsg;
use think\Db;
class Goods extends Base {
    private $user_id; // 用户ID
    private $page; // 页码
    private $create_time;

    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id, $time, $value, $reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301, 'msg'=>$this->msg_language->otherLogin[$this->language]]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->msg_language->frost[$this->language]]);
            exit;
        }
    }
    
    /**
     * 商品分类
     * @return \think\response\Json
     */
    public function goodsType(){
        $res = Db::table('up_goods_type')
            ->field('gt_id, gt_thumb, gt_name')
            ->where('gt_del', 1)
            ->order('gt_time desc')
            ->select();
        if($res){
            return json(['status' => 200, 'msg' => $res]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 商品列表
     * @return \think\response\Json
     * type        1 集市   2 其他分类
     * price       0 不排序   1 价格升序   2 价格降序
     * sail_num    0 不排序   1 销量升序   2 销量降序
     * high        0 不排序   1 好评升序   2 好评降序
     * gt_id       分类ID
     */
    public function goodsList(){
        $type = input('type'); // 1 集市   2 其他分类
        $price = input('price'); // 0 不排序  1 价格升序   2 价格降序
        $sail_num = input('sail_num'); // 0 不排序   1 销量升序    2 销量降序
        $high = input('high'); // 0 不排序   2 好评升序   2 好评降序
        $gt_id = input('gt_id');
        $order = 'goods_time desc';
        $where['goods_type'] = 1;
        $where['goods_status'] = 1;
        if($type == 2){
            $high_painter = Db::table('up_u_user')
                ->where('user_status', 1)
                ->where('user_role', 'IN', '4,5,6')
                ->column('user_id');
            $where['goods_user_id'] = ['IN', $high_painter];
            $where['goods_cate'] = $gt_id;
        }
        if($price != 0){
            if($price == 1){
                $order = 'goods_spec_min_price asc';
            }else{
                $order = 'goods_spec_min_price desc';
            }
        }
        if($sail_num != 0){
            if($sail_num == 1){
                $order = 'goods_sail_num asc';
            }else{
                $order = 'goods_sail_num desc';
            }
        }
        if($high != 0){
            if($high == 1){
                $order = 'goods_high asc';
            }else{
                $order = 'goods_high desc';
            }
        }
        
        $res = Db::table('up_goods')
            ->where($where)
            ->field('goods_id, goods_name, goods_thumb')
            ->page($this->page, 15)
            ->order($order)
            ->select();
        if($res){
            foreach ($res as $key => $vo){
                $spec_price =  Db::table('up_goods_spec')
                    ->where('spec_goods_id', $vo['goods_id'])
                    ->order('spec_price asc')
                    ->find();
                $res[$key]['spec_price'] = $spec_price['spec_price'];
                $res[$key]['spec_old_price'] = $spec_price['spec_old_price'];
            }
            return json(['status' => 200, 'msg' => $res]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
}